﻿using Common;
using DadayunSync.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DadayunSync
{
    public partial class Form1 : Form
    {
        delegate void ShowResponse(HttpResponseMessage response);
        

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        AccessToken accessToken;

        string client_id = "335610974f804cfdb1e730dc85978137";
        string client_secret = "2bb9a72a7d844f8db64573d31a4ef6cd";
        
        string username = "userAdmin";
        string password = "abc123";
        string baseUri = "https://api.dadayun.cn";

        string token = "14d1e7ee528f5799b860b52f49850cecee41e39edebd97827addeb617d57ddaf";

        private  void button1_Click(object sender, EventArgs e)
        {
            
        }

       

        private void tbGetToken_Click(object sender, EventArgs e)
        {
            var uri = "/connect/token";
            var content = new FormUrlEncodedContent(new Dictionary<string, string>()
                {
                       {"client_id", client_id},
                       {"client_secret", client_secret},
                       {"grant_type", "password"},
                       {"username", username},
                       {"password", password}
                });
            HttpClientHelper. HttpClientPostAsync(baseUri, uri, content,ShowToken,
                delegate(HttpResponseMessage resp,string errJson) {
                    textBox1.Text = resp.StatusCode.ToString();
                    textBox1.Text += "\r\n" + resp.RequestMessage;
                    textBox1.Text += "\r\n" + errJson;
                });
        }

        private void ShowToken(string json)
        {
            accessToken = JsonConvert.DeserializeObject<AccessToken>(json);
            textBox1.Text = "Token: " + accessToken.Access_token;
            textBox1.Text += "\r\nExpired: " + accessToken.Expires_in;
            textBox1.Text += "\r\nRefreshToken: " + accessToken.Refresh_token;
            textBox1.Text += "\r\nTokenType: " + accessToken.Token_type;
        }

        private void tbGetServerTime_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> headers = new Dictionary<string, string>();
            headers.Add("Authorization", "Bearer " + token);
            var uri =  "/v1/date";
            var ans = HttpClientHelper.HttpClientGetSync(baseUri, uri, out string json, headers);
            if(ans==HttpStatusCode.OK)
            {

                string ts = json.Substring(1, json.Length - 2);
                DateTime dateTime = DateTime.Parse(ts);
                
                textBox1.Text = dateTime.ToString();
                
            }
            else
            {
                textBox1.Text = ans.ToString();
                textBox1.Text += json;
            }
        }
        

        private void tbGetDictionaryItems_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> headers = new Dictionary<string, string>();
            headers.Add("Authorization", "Bearer " + token);
            var uri =  "/v1/data/dictionary_types/" + txtData.Text + "/datas";
            var ans = HttpClientHelper.HttpClientGetSync(baseUri, uri, out string json, headers);
            if (ans == HttpStatusCode.OK)
            {
                var dicts= JsonConvert.DeserializeObject<List< MyClass>>(json);
                textBox1.Text = json;
            }
            else
            {
                textBox1.Text = ans.ToString();
                textBox1.Text += json;
            }
        }
        class MyClass
        {
            public string Id { get; set; }
            public string Name { get; set; }
        }
        private void tbGetRoles_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> headers = new Dictionary<string, string>();
            headers.Add("Authorization", "Bearer " + token);
            var uri = "/v1/roles";
            var ans = HttpClientHelper.HttpClientGetSync(baseUri, uri, out string json, headers);
            if (ans == HttpStatusCode.OK)
            {
                textBox1.Text = json;
            }
            else
            {
                textBox1.Text = ans.ToString();
                textBox1.Text += json;
            }
        }

        private void tbGetRoleUsers_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> headers = new Dictionary<string, string>();
            headers.Add("Authorization", "Bearer " + token);
            var uri = "/v1/roles/"+txtData.Text+"/users";
            var ans = HttpClientHelper.HttpClientGetSync(baseUri, uri, out string json, headers);
            if (ans == HttpStatusCode.OK)
            {
                textBox1.Text = json;
            }
            else
            {
                textBox1.Text = ans.ToString();
                textBox1.Text += json;
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            Dictionary<string, string> headers = new Dictionary<string, string>();
            headers.Add("Authorization", "Bearer " + token);
            var uri = "/v1/roles";
            var ans = HttpClientHelper.HttpClientGetSync(baseUri, uri, out string json, headers);
            if (ans==HttpStatusCode.OK)
            {
                textBox1.Text = json;
            }
            else
            {
                textBox1.Text = ans.ToString();
                textBox1.Text += "\r\n"+json;
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> headers = new Dictionary<string, string>();
            headers.Add("Authorization", "Bearer " + token);
            var uri =  "/v1/roles";
           
            HttpClientHelper.HttpClientGetAsync(baseUri, uri,//简单处理方法
                 delegate (string jsonStr) {
                     textBox1.Text = jsonStr;
                 }, delegate (HttpResponseMessage resp,string errJson) {
                     textBox1.Text = resp.StatusCode.ToString();
                     textBox1.Text += "\r\n" + resp.RequestMessage;
                     textBox1.Text += "\r\n" + errJson;
                 }, headers);
        }

       

        private void tbRefreshToken_Click(object sender, EventArgs e)
        {
            if (accessToken == null) return;
            var uri =  "/connect/token";
            var content = new FormUrlEncodedContent(new Dictionary<string, string>()
                {
                       {"client_id", client_id},
                       {"client_secret", client_secret},
                       {"grant_type", "refresh_token"},
                       {"refresh_token", accessToken.Refresh_token}
                });
            HttpClientHelper.HttpClientPostAsync(baseUri, uri, content, ShowToken,
                delegate (HttpResponseMessage resp,string errJson) {
                    
                    textBox1.Text = resp.StatusCode.ToString();
                    //textBox1.Text += "\r\n" + resp.RequestMessage;
                    textBox1.Text += "\r\n" + errJson;
                });
        }

        private void tbGetReportTemplates_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> headers = new Dictionary<string, string>();
            headers.Add("Authorization", "Bearer " + token);
            string uri = "/v1/report/templates/"+txtData.Text;
            var ans = HttpClientHelper.HttpClientGetSync(baseUri , uri, out string json, headers);
            if (ans == HttpStatusCode.OK)
            {
                textBox1.Text = json;
            }
            else
            {
                textBox1.Text = ans.ToString();
                textBox1.Text += json;
            }
        }

        private void tbGetReportDatas_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> headers = new Dictionary<string, string>();
            headers.Add("Authorization", "Bearer " + token);
            string uri = "/v1/report/templates/" + txtData.Text+"/search";
            if(!string.IsNullOrEmpty(txtFilter.Text))
            {
                uri += "?Filter=" + txtFilter.Text;
            }
            var ans = HttpClientHelper.HttpClientGetSync(baseUri, uri, out string json, headers);
            if (ans == HttpStatusCode.OK)
            {
                textBox1.Text = json;
            }
            else
            {
                textBox1.Text = ans.ToString();
                textBox1.Text += json;
            }
        }
    }
}
